package com.koron.operation.classes.shift.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.operation.classes.shift.bean.po.ClassesDutyRecord;
import com.koron.operation.classes.shift.bean.query.DutyRecordQuery;
import com.koron.operation.classes.shift.service.ClassesDutyRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * 值班记录(ClassesDutyRecord)表控制层
 *
 * @author jzp
 * @since 2022-04-01 11:37:38
 */
@Api(tags = "值班记录接口")
@RestController
@RequestMapping("duty")
public class DutyRecordController {
    /**
     * 服务对象
     */
    @Autowired
    private ClassesDutyRecordService classesDutyRecordService;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping
    public Response<ClassesDutyRecord> selectOne(@RequestParam String id) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), classesDutyRecordService, "getById", ClassesDutyRecord.class, id));
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("page")
    public Response<PageInfo<ClassesDutyRecord>> page(PageQuery pageQuery, DutyRecordQuery query) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), classesDutyRecordService, "page", PageInfo.class, pageQuery, query));
    }

    @SneakyThrows
    @ApiOperation(value = "导出")
    @GetMapping("export")
    public void exportFile(HttpServletResponse response, DutyRecordQuery query) {
        List<ClassesDutyRecord> list = ADOConnection.runTask(getCurrDs(), classesDutyRecordService, "list", List.class, query);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("值班记录", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ClassesDutyRecord.class)
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .sheet("值班记录")
                .doWrite(list);
    }

}
